<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="iscollapse ? '64px':'200px'">
        <div class="toggle-button" @click="toggleButton">|||</div>
        <!-- 侧边栏菜单区域   属性最多写九个-->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标  一级菜单的类需要动态绑定-->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文字 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/'+ subItem.path " @click="saveNavState('/' + subItem.path)">
            <!-- :index="'/'+ subItem.id " 跳转到/id路径 修改为path属性-->
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文字 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区域 -->
      <el-main>
        <!-- welcome组件的路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
    //data里的数据具有响应式
    data() {
        return {
            //接收菜单栏的数据
            menulist:[],
            //定义一个对象用来存放一级菜单的每一项的id与之对应的图标样式
            iconsObj:{
              //属性名:属性值
              '125':'iconfont icon-user',
              '103':'iconfont icon-tijikongjian',
              '101':'iconfont icon-shangpin',
              '102':'iconfont icon-danju',
              '145':'iconfont icon-baobiao'
            },
            //是否折叠
            iscollapse : false,
            //导航栏被激活的导航地址
            activePath:''
        }
    },
  //created钩子函数 页面一加载就调用
  created() {
    this.getMenuList();
  },
  //让导航栏处于被激活状态的第二种方法,也解决了方法一的duge问题
  //组件更新之前会立即执行beforeUpdate
  beforeUpdate () {
    // console.log('当前路由的path:',this.$route.path);  
    this.activePath = this.$route.path
    //当路径为/goods/add 时让二级菜单商品列表高亮
    if (this.$route.path === '/goods/add') {
      this.activePath = '/goods'
    }
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有的菜单
    async getMenuList() {
      //发送axios异步请求
    //   const res = await this.$http.get("menus");
      //console.log(res); //Promise {<pending>} 使用await获取异步请求的结果
      const {data:res} = await this.$http.get("menus");
    //   console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //接收数据成功,将数据赋值给data中的menulist实现数据的双向绑定
        this.menulist = res.data
    },
    //点击按钮切换折叠与展开
    toggleButton() {
      this.iscollapse = !this.iscollapse
    },
    //保存二级菜单index的值 方法一
    saveNavState(activePath) {
      //将index也就是当前激活状态 保存到sessionStorage里
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
};
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
//让整个布局占满全屏
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 20px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.3em;
  cursor: pointer;
}
</style>
